﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ciripescu_WebRole.BL;
using Ciripescu_WebRole.Entities;

namespace Ciripescu_WebRole.Admin
{
    public partial class impersonate : AdminBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            User user = null;
            try
            {
                string userName = Request.QueryString["userName"];
                UserManager um = new UserManager();
                user = um.GetItem(userName);
                if (user == null) throw new Exception();
                Utils.WriteLog(EventLogName.Information,"Admin " + (string)Session["UserName"] + " is impersonating " + user.GetUserName());

                Session.Clear();

                Session.Add("UserName", user.GetUserName());
                //Session.Add("Group", user.GroupId);
                Session.Add("Impersonation", true);
            }            
            catch (Exception ex)
            {
                
                return;
            }
            Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Redirect("/home/");
            // admins should not be impersonated!
            //if (user.GroupId == (int)GroupsEnum.Administrator) Response.Redirect("admin/admin.aspx");
        }
    }
}
